package com.samsungmobile.security;

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

import com.samsungmobile.common.Logger;

public class UKDESEncryptDecrypt { 
	
	private DESedeKeySpec desKeySpec;
    private SecretKeyFactory keyFac;
    private SecretKey pbeKey;
	private Cipher pbeCipher;
	private final String strKey = "QWERTYUIOP!@#$%^&*()aaaa";
/*
	��ȣȭ ��� ��� :DESede/ECB/NoPadding
	��ȣȭ ��� : JCE Library
	��ȣȭ �� hexastring 8�� ��ȯ�� parameter �ְ��=.
*/	
	/*
		��ȣȭ�� ��ȣȭ�� '�� �޼ҵ� d��.
	*/
	public UKDESEncryptDecrypt() {
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		try {
			desKeySpec = new DESedeKeySpec(strKey.getBytes());
			keyFac = SecretKeyFactory.getInstance("DESede");
			pbeKey = keyFac.generateSecret(desKeySpec);
			pbeCipher = Cipher.getInstance("DESede/ECB/NoPadding");
		}
		catch(Exception e) {
			Logger.printError(e);
		}
	}
	/*
		��ȣȭ �޼ҵ�.
	*/
	public String encrypt(String str) {
		byte[] b = toMultiple8(str);
		String strReturn = "";
		
		try {
			pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey);
			byte[] encrypted = pbeCipher.doFinal(b);
			strReturn =  toHexString(encrypted);
			return strReturn;
		}
		catch(Exception e) {
			Logger.printError(e);
			return "*********************";
		}
		
		//return strReturn;
	}
	/*
		��ȣȭ �޼ҵ�.
	*/
	public String decrypt(String str) {
		
		try {
		byte[] b = toBytes(str);;
		String strReturn = "";
			pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey);
			byte[] decrypted = pbeCipher.doFinal(b);
			strReturn = new String(decrypted);
			return strReturn.trim();
		}
		catch(Exception e) {
			Logger.printError(e);
			return "*********************";
		}
		
		//return strReturn.trim();
	}
	/*
		��ȣȭ�� data �� 8�� ���� ����.
	*/
	private byte[] toMultiple8(String str) {
		int j = str.length() % 8;
		if(j != 0) {
			for(int i = 0; i < 8 - j; i++)
				str += " ";
		}
		
		return str.getBytes();
	}

/***	
	private String bytesToHex(byte[] data) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < data.length; i++) {
			//buf.append(byteToHex(data[i]).toUpperCase());
			buf.append(byteToHex(data[i]));
		}
		return (buf.toString());
	}
	
	private String byteToHex(byte data) {
		StringBuffer buf = new StringBuffer();
		buf.append(toHexChar((data >>> 4) & 0x0F));
		buf.append(toHexChar(data & 0x0F));
		return buf.toString();
	}
	
	private char toHexChar(int i) {
		if ((0 <= i) && (i <= 9)) {
			return (char) ('0' + i);
		}
		else {
			return (char) ('a' + (i - 10));
		}
	}
****/
	/*
		hex character �� �����ϱ� '�� �Լ�.
	*/
    private void byte2hex(byte b, StringBuffer buf) {
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                            '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        int high = ((b & 0xf0) >> 4);
        int low = (b & 0x0f);
        buf.append(hexChars[high]);
        buf.append(hexChars[low]);
    }
	/*
		hex string 8�� ����.
	*/
    private String toHexString(byte[] block) {
        StringBuffer buf = new StringBuffer();

        int len = block.length;

        for (int i = 0; i < len; i++) {
             byte2hex(block[i], buf);
        } 
        return buf.toString();
    }
 	/*
		string ; byte ��8�� ����.
	*/   
    private byte[] toBytes(String s) {
    	byte[] b = new byte[s.length() / 2];
    	for(int i = 0; i < s.length() / 2; i++) {
			String ss = s.substring(i*2, (i*2)+2);
			b[i] = (byte)((Byte.parseByte(ss.substring(0, 1), 16) << 4) | Byte.parseByte(ss.substring(1, 2), 16));
    	}
    	
    	return b;
    }
}
